package com.example.manage_c20086.api;
import com.example.manage_c20086.cutil.cTool;
import com.pojo.Entity.Site;
import com.pojo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping("/api/site")
public class siteApi {
    @Autowired
    private RestTemplate restTemplate;

    private static final String REST_URL_PREFIX="http://SITE-MANAGEMENT/site";
    private static final String IDENTIFYING="identifying";
    @GetMapping("/getlsite")
    public Result<?> getlsite(@RequestParam Integer page, @RequestParam Integer number){
        return restTemplate.getForObject(REST_URL_PREFIX+"/getlsite/"+page+"/"+number,Result.class);
    }

    @GetMapping("/getsite")
    public Result<?>getsite(@RequestParam Integer sID){
      return   restTemplate.getForObject(REST_URL_PREFIX+"/getsite/"+sID,Result.class);
    }

    @PostMapping("/update")
    public Result<?>update( Site site, HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        cTool.jsonToMap(site,map);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return restTemplate.postForObject(REST_URL_PREFIX+"/update",request,Result.class);
    }
    @PostMapping("/insert")
    public Result<?>insert( Site site, HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        cTool.jsonToMap(site,map);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return restTemplate.postForObject(REST_URL_PREFIX+"/insert",request,Result.class);
    }
    @PostMapping("/presite")
    public Result<?>presite(@RequestParam String vBegintime,
                            @RequestParam String vEndtime,
                            @RequestParam String vDate,
                            @RequestParam Integer sID,
                             HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("vBegintime",vBegintime);
        map.add("vEndtime",vEndtime);
        map.add("vDate",vDate);
        map.add("sID",sID);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return restTemplate.postForObject(REST_URL_PREFIX+"/presite",request,Result.class);
    }


    @GetMapping("/getsdate")
    public Result<?>getsdate(@RequestParam String vDate,@RequestParam Integer sID){
        System.out.println(vDate);
        return  restTemplate.getForObject(REST_URL_PREFIX+"/getsdate/"+vDate+"/"+sID,Result.class);
    }

    @PostMapping("/payment")
    public Result<?>payment(@RequestParam Integer vID,@RequestParam Integer vPaymentStatus,HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("vID",vID);
        map.add("vPaymentStatus",vPaymentStatus);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return restTemplate.postForObject(REST_URL_PREFIX+"/payment",request,Result.class);
    }
    @GetMapping("getvr")
    public Result<?>getvr(HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return restTemplate.postForObject(REST_URL_PREFIX+"/getvr",request,Result.class);
    }
    @GetMapping("delete")
    public Result<?>delete(Integer sID,HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("sID",sID);
        System.out.println("sdelete"+sID);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return restTemplate.postForObject(REST_URL_PREFIX+"/delete",request,Result.class);
    }

}
